import { message } from 'antd';
import {ctx} from '../WebGLRenderer';
import KeyMirror from 'keymirror';
import { Point3d } from '../math/Point3d';
const msgType = KeyMirror({
  GetPoint: null,
  Escape: null, // 退出按键
});
// 返回：point3d实例或false
export function GetPoint(msg){
  const promise = new Promise((resolve, reject)=>{
    message.info({content:msg, key: msgType.GetPoint, duration: 0});
    ctx.canvas.addEventListener('click', handleClick);
    document.addEventListener('keydown', handleKeyDown);
    function handleClick(ev){
      message.destroy(msgType.GetPoint);
      const pt = getWebglPt(ev, ctx.canvas);
      ctx.canvas.removeEventListener('click', handleClick);
      document.removeEventListener('keydown', handleKeyDown);
      return resolve(pt);
    }
    function handleKeyDown(ev) {
      const { key } = ev;
      if(key === msgType.Escape) {
        message.destroy(msgType.GetPoint);
        ctx.canvas.removeEventListener('click', handleClick);
        document.removeEventListener('keydown', handleKeyDown);
        return resolve(false);
      }
    }
  })
  return promise;
}
let movePt = new Proxy({}, {
  get: function (target, propKey, receiver) {
    console.log(`getting ${propKey}!`);
    return Reflect.get(target, propKey, receiver);
  },
  set: function (target, propKey, value, receiver) {
    console.log(`setting ${propKey}!`);
    return Reflect.set(target, propKey, value, receiver);
  }
})
export function GetMovePoint(msg){
  document.addEventListener('mousemove', handleMove);
  function handleMove(ev){
    movePt = getWebglPt(ev, ctx.canvas);
    console.log('movePt: ', movePt);
  }
}

function getWebglPt(ev, canvas) {
  let x = ev.clientX; // x coordinate of a mouse pointer
  let y = ev.clientY; // y coordinate of a mouse pointer
  let rect = ev.target.getBoundingClientRect() ;
  x = ((x - rect.left) - canvas.width/2)/(canvas.width/2);
  y = (canvas.height/2 - (y - rect.top))/(canvas.height/2);
  return new Point3d(x, y);
}

